<?php

Class MonitoringActivitiesController extends AppController {

    public $name = 'MonitoringActivities';

    public function index($operating_plan_id) {
        $this->layout = "ajax";
        //envío el código del proyecto
        $this->set("codigo", $this->Session->read('cod'));
        $this->set("operatingPlanId", $operating_plan_id);
        //envío las actividades
        $this->MonitoringActivity->recursive = -1;
        $this->paginate = array('MonitoringActivity' => array('maxLimit' => 20, 'limit' => 20, 'order' => array('id' => 'DESC'), 'fields' => array('MonitoringActivity.id', 'MonitoringActivity.fecha_programacion', 'MonitoringActivity.fecha_real', 'MonitoringActivity.objetivo', 'MonitoringActivity.presupuesto')));
        $this->set('MonitoringActivities', $this->paginate(array('MonitoringActivity.operating_plan_id' => $operating_plan_id)));
    }

    public function index_planeacion($operating_plan_id) {
        $this->layout = "ajax";
        //envío el código del proyecto
        $this->set("codigo", $this->Session->read('cod'));
        $this->set("operatingPlanId", $operating_plan_id);
        //envío las actividades
        $this->MonitoringActivity->recursive = -1;
        $this->paginate = array('MonitoringActivity' => array('maxLimit' => 20, 'limit' => 20, 'order' => array('id' => 'DESC'), 'fields' => array('MonitoringActivity.id', 'MonitoringActivity.fecha_programacion', 'MonitoringActivity.fecha_real', 'MonitoringActivity.objetivo', 'MonitoringActivity.presupuesto')));
        $this->set('MonitoringActivities', $this->paginate(array('MonitoringActivity.operating_plan_id' => $operating_plan_id)));
    }

    public function add($operating_plan_id, $tipo) {
        $this->layout = "ajax";
        $this->set('tipo', $tipo);
        if (empty($this->data)) {
            $this->set('operating_plan_id', $operating_plan_id);
        } else {
            if ($this->MonitoringActivity->saveAll($this->data)) {
                $this->Session->setFlash('Datos guardados correctamente', 'flash_custom');
                switch ($tipo) {
                    case 1:
                        $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index_planeacion', $operating_plan_id));
                        break;
                    case 2:
                        $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index', $operating_plan_id));
                        break;

                    default:
                        break;
                }
            } else {
                $this->Session->setFlash('Error guardando datos', 'flash_custom');
                switch ($tipo) {
                    case 1:
                        $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index_planeacion', $operating_plan_id));
                        break;
                    case 2:
                        $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index', $operating_plan_id));
                        break;
                    default:
                        break;
                }
            }
        }
    }

    public function edit($id, $tipo) {
        $this->layout = "ajax";
        $this->set('tipo', $tipo);
        $this->MonitoringActivity->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->MonitoringActivity->find("first", array("conditions" => array("MonitoringActivity.id" => $id)));
        } else {
            if ($this->MonitoringActivity->saveAll($this->data)) {
                $this->Session->setFlash('Datos guardados correctamente', 'flash_custom');
                switch ($tipo) {
                    case 1:
                        $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index_planeacion', $this->data['MonitoringActivity']['operating_plan_id']));
                        break;
                    case 2:
                        $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index', $this->data['MonitoringActivity']['operating_plan_id']));
                        break;

                    default:
                        break;
                }
            } else {
                $this->Session->setFlash('Error guardando datos', 'flash_custom');
                switch ($tipo) {
                    case 1:
                        $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index_planeacion', $this->data['MonitoringActivity']['operating_plan_id']));
                        break;
                    case 2:
                        $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index', $this->data['MonitoringActivity']['operating_plan_id']));
                        break;

                    default:
                        break;
                }
            }
        }
    }

    public function delete($id, $operating_plan_id) {
        if ($this->MonitoringActivity->delete($id)) {
            $this->Session->setFlash('Información de la visita borrada con éxito', 'flash_custom');
            $this->redirect(array('controller' => 'MonitoringActivities', 'action' => 'index_planeacion', $operating_plan_id));
        }
    }

    public function file_index($monitoring_activity_id) {
        $this->set('monitoring_activity_id', $monitoring_activity_id);
        $operating_plan_id = $this->MonitoringActivity->field('MonitoringActivity.operating_plan_id', array('MonitoringActivity.id' => $monitoring_activity_id));
        $proyect_id = $this->MonitoringActivity->OperatingPlan->field('OperatingPlan.proyect_id', array('OperatingPlan.id' => $operating_plan_id));
        $this->set('codigo', $this->MonitoringActivity->OperatingPlan->Proyect->field('Proyect.codigo', array('Proyect.id' => $proyect_id)));
        $this->set('operating_plan_id', $operating_plan_id);
    }

    public function add_file($monitoring_activity_id, $tipo) {
        $this->layout = "ajax";
        $this->set('monitoring_activity_id', $monitoring_activity_id);
        $this->set('tipo', $tipo);
        $this->MonitoringActivity->recursive = -1;
        if (empty($this->data)) {
            $this->data = $this->MonitoringActivity->find("first", array("conditions" => array("MonitoringActivity.id" => $monitoring_activity_id), 'fields' => array('MonitoringActivity.id', 'MonitoringActivity.operating_plan_id')));
        } else {
            if ($this->MonitoringActivity->saveAll($this->data)) {

                $proyect_id = $this->MonitoringActivity->OperatingPlan->field('OperatingPlan.proyect_id', array('OperatingPlan.id' => $this->data['MonitoringActivity']['operating_plan_id']));
                $codigo = $this->MonitoringActivity->OperatingPlan->Proyect->field('Proyect.codigo', array('Proyect.id' => $proyect_id));

                $nombreArchivo = "";
                switch ($tipo) {
                    case 1:
                        $nombreArchivo = "INFORME-VISITA-$codigo-" . $monitoring_activity_id;
                        $this->data['MonitoringActivity']['informe_visita'] = "files/$codigo/Seguimiento/" . $nombreArchivo;
                        break;
                    case 2:
                        $nombreArchivo = "REGISTRO-FOTOGRAFICO-$codigo-" . $monitoring_activity_id;
                        $this->data['MonitoringActivity']['registro_fotografico'] = "files/$codigo/Seguimiento/" . $nombreArchivo;
                        break;
                    case 3:
                        $nombreArchivo = "LEGALIZACION-COMISION-$codigo-" . $monitoring_activity_id;
                        $this->data['MonitoringActivity']['legalizacion_comision'] = "files/$codigo/Seguimiento/" . $nombreArchivo;
                        break;
                    case 4:
                        $nombreArchivo = "LISTADO-ASISTENCIA-$codigo-" . $monitoring_activity_id;
                        $this->data['MonitoringActivity']['listado_asistencia'] = "files/$codigo/Seguimiento/" . $nombreArchivo;
                        break;

                    default:
                        $this->Session->setFlash('La opción no corresponde a un archivo valido', 'flash_custom');
                        $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
                        break;
                }

                $nombreArchivo = $nombreArchivo . ".pdf";
                if ($this->MonitoringActivity->saveAll($this->data)) {
                    if ($this->data['MonitoringActivity']['archivo']['tmp_name'] != "") {
                        $rutaArchivo = APP . "webroot" . "/" . "files" . "/" . $codigo . "/Seguimiento";
                        if (!is_dir($rutaArchivo)) {
                            //se crea la carpeta si no existe
                            if (!mkdir($rutaArchivo)) {
                                echo "error creando archivo";
                                $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
                            }
                        }

                        $rutaArchivo.= "/" . $nombreArchivo;
                        if (move_uploaded_file($this->data['MonitoringActivity']['archivo']['tmp_name'], $rutaArchivo)) {
                            $this->Session->setFlash('Archivo cargado con éxito', 'flash_custom');
                            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
                        } else {
                            $this->Session->setFlash('No es posible cargar el archivo', 'flash_custom');
                            $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
                        }
                    } else {
                        $this->Session->setFlash('No se cargo ningún archivo.', 'flash_custom');
                        $this->redirect(array('controller' => 'Pages', 'action' => 'display'));
                    }
                } else {
                    $this->Session->setFlash('Error guardando datos');
                }
            }
        }
    }

}

?>
